package com.example.wanbaqian.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.wanbaqian.R;
import com.example.wanbaqian.adapter.FindMemberAdapter;
import com.example.wanbaqian.base.BaseFragment;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.base.RefreshCallback;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.bean.MemberInfo;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.SPUtils;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 发现fragment.
 */
public class FindChildFragment extends BaseFragment implements HttpCallback, RefreshCallback {
    private static final String ARG_PARAM1 = "param1";
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    EmptyWrapper emptyWrapper;
    private List<MemberInfo> mList = new ArrayList<>();
    private FindReceiver findReceiver;
    private String title = "";

    public static FindChildFragment newInstance(String param1) {
        FindChildFragment fragment = new FindChildFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_findlist;
    }

    @Override
    protected void initViewsAndEvents(View view) {
        title = getArguments().getString(ARG_PARAM1);
        emptyWrapper = new EmptyWrapper(new FindMemberAdapter(mContext, mList));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        View emptyView =  LayoutInflater.from(mContext).inflate(R.layout.view_empty_activits,  (ViewGroup)getActivity().findViewById(R.id.mRecyclerView),false);
        TextView emptyTv = (TextView) emptyView.findViewById(R.id.emptyTv);
        emptyTv.setText(title.equals("我的会员") ? "您暂无会员，马上邀请吧~" : "暂无排行榜数据~");
        emptyTv.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(getActivity(), R.drawable.empty_vip), null, null);
        emptyWrapper.setEmptyView(emptyView);
        mRecyclerView.setAdapter(emptyWrapper);
        setHeaderAndFooter(R.id.springview, false, true, this);
        springView.setEnable(false);
        regReceiver();
    }

    private void regReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(CommonValue.FindFragmentReceiver);
        findReceiver = new FindReceiver();
        getActivity().registerReceiver(findReceiver, intentFilter);
    }

    @Override
    protected void onFirstUserVisible() {
        getDate();
    }

    private void getDate() {
        HttpUtils.getInstance().post(
                GetJsonUtils.getInstance().getRequest(title.equals("我的会员") ? HttpURL.MY_MEMBER : HttpURL.AGENT_RANK,
                        mContext, new String[]{"accessid", "cur_page", "page_size"},
                        new Object[]{SPUtils.getAccessid(mContext), page, count})
                , title.equals("我的会员") ? HttpURL.MY_MEMBER : HttpURL.AGENT_RANK, CommonValue.REQUEST_CODE1, this);
    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    protected void DetoryViewAndThing() {
        getActivity().unregisterReceiver(findReceiver);
    }

    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        switch (requestCode) {
            case CommonValue.REQUEST_CODE1:
                if (page == 1) {
                    mList.clear();
                }
                List<MemberInfo> newmList = JsonUtils.getInstance().getList(content, title.equals("我的会员") ? "member_list" : "agent_list", MemberInfo.class);
                mList.addAll(newmList);
                canLoadmore = JsonUtils.getInstance().havePage(content);
                emptyWrapper.notifyDataSetChanged();
                springView.onFinishFreshAndLoad();
                break;
        }
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }

    @Override
    public void onError(Info info, int requestCode) {
        springView.onFinishFreshAndLoad();
        showToast(info.getMsg());
    }

    @Override
    public void getNetDate() {
        getDate();
    }

    class FindReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            springView.setEnable(intent.getExtras().getBoolean("state"));
        }
    }
}
